<?php

	// Cache - does also cache the search...
	require_once('dis_cache.php');

	$LOCATION_BY_URL = @unserialize(@file_get_contents('../data/location_by_url.dat'));

	$path = explode('/', trim($_SERVER['REDIRECT_URL'], '/'));
	
	$last = '';
	
	while(count($path) > 0){
		$url = implode('/', $path);
		if(isset($LOCATION_BY_URL[$url])){
			break;
		}
		$last = array_pop($path);
	}
	
	if(count($path) == 0){
		// no location entered/found -> return to base location of mandant
		$MANDANT = @unserialize(@file_get_contents('../data/mandant.dat'));
		if(
			!isset($_SERVER['SERVER_NAME']) ||
			!is_array($MANDANT) || !isset($MANDANT[$_SERVER['SERVER_NAME']])
		){
			$ERROR404 = 'no mandant';
			require_once('dis_404.php');
		}

		$LOCATIONS = @unserialize(@file_get_contents('../data/location/'.$MANDANT[$_SERVER['SERVER_NAME']][2].'.dat'));
		header('Location: '.$LOCATIONS['*LINK-ORT*']);
		die();
	}
	if($last == 'suche.html'){
		$location_id = $LOCATION_BY_URL[$url];
		require_once('dis_search.php');
	}else{
		$type = 'o';
		$type_id = $LOCATION_BY_URL[$url];
		$PAGE = 0;
		$canonical_url = '/'.$url.'/';
		if(preg_match('!^seite([0-9]+).html$!', $last, $r)){
			$PAGE = max(0, $r[1]-1);
		}
		if($PAGE > 0){
			$canonical_url .= 'seite'.($PAGE+1).'.html';
		}
		if($_SERVER['REQUEST_URI'] != $canonical_url){
			header('HTTP/1.1 301 Moved Permanently');
			header('Location: '.$canonical_url);
			die();
		}
		$PICTOOLS_LOG = 'ort;'.$type_id;
		require_once('dis_list.php');
	}

?>